import dayjs from 'dayjs'
import { useI18n } from 'vue-i18n'

export default function () {
    const { t } = useI18n()

    // 日期时间快捷选项
    const rangeTimeShortcuts = [
        {
            text: t('other.today'),
            value: () => {
                return getAroundTodayRangeTime()
            }
        },
        {
            text: t('other.thisWeek'),
            value: () => {
                return getAroundCurrentWeekRangeTime()
            }
        },
        {
            text: t('other.lastWeek'),
            value: () => {
                return getAroundLastWeekRangeTime()
            }
        },
        {
            text: t('other.thisMonth'),
            value: () => {
                return getAroundCurrentMonthRangeTime()
            }
        },
        // {
        //     text: t('other.lastMonth'),
        //     value: () => {
        //         return getAroundLastMonthRangeTime()
        //     }
        // },
        // {
        //     text: t('other.thisQtr'),
        //     value: () => {
        //         return getAroundCurrentQuarterRangeTime()
        //     }
        // },
        {
            text: t('other.lastQtr'),
            value: () => {
                return getAroundCurrentYearRangeTime()
            }
        }
    ]

    return {
        rangeTimeShortcuts
    }
}

const formatDate = (date) => {
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0') // Months are zero-based
    const day = String(date.getDate()).padStart(2, '0')
    const hours = String(date.getHours()).padStart(2, '0')
    const minutes = String(date.getMinutes()).padStart(2, '0')
    const seconds = String(date.getSeconds()).padStart(2, '0')

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

/**
 * 获取当前月份的第一天
 * @returns {string}
 */
export const getCurrentMonthFirst = () => {
    const now = new Date()
    const nowMonth = now.getMonth()
    const nowYear = now.getFullYear()
    // 本月的开始时间
    return new Date(nowYear, nowMonth, 1)
}

/**
 * 获取当前月份的最后一天
 * @returns {string}
 */
export const getCurrentMonthLast = () => {
    const now = new Date()
    const nowMonth = now.getMonth()
    const nowYear = now.getFullYear()
    // 本月的结束时间
    return new Date(nowYear, nowMonth + 1, 0)
}

/**
 * 获取日期的开始时间
 * @returns {Date}
 */
export const getDateStartTime = (date) => {
    if (!date) {
        date = new Date()
    }
    if (typeof date == 'number') {
        date = new Date(date)
    }
    // 开始时间
    const startTime = new Date(date.getFullYear(), date.getMonth(), date.getDate())

    return startTime
}

/**
 * 获取日期的结束时间
 * @returns {Date}
 */
export const getDateEndTime = (date) => {
    if (!date) {
        date = new Date()
    }
    if (typeof date == 'number') {
        date = new Date(date)
    }
    // 结束时间
    const endTime = new Date(date.getFullYear(), date.getMonth(), date.getDate())
    endTime.setHours(23)
    endTime.setMinutes(59)
    endTime.setSeconds(59)

    return endTime
}

/**
 * 获取近七天的日期时间
 * @returns {string[]}
 */
export const getAround7RangeTime = () => {
    const now = new Date()
    const nowYear = now.getFullYear()
    let nowMonth = now.getMonth() + 1
    // 个位补0
    nowMonth = nowMonth < 10 ? '0' + nowMonth : nowMonth
    const nowDate = now.getDate() < 10 ? '0' + now.getDate() : now.getDate()

    const start = new Date(nowYear, nowMonth, nowDate - 7)
    // 个位补0
    const startDate = start.getDate() < 10 ? '0' + start.getDate() : start.getDate()
    const rangeTime = [
        `${nowYear}-${nowMonth}-${startDate} 00:00:00`,
        `${nowYear}-${nowMonth}-${nowDate} 23:59:59`
    ]
    console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取日期的开始时间和结束时间
 * @returns {[startTime, endTime]}
 */
export const getDateRangeTime = (date) => {
    if (!date) {
        date = new Date()
    }
    if (date instanceof Number) {
        date = new Date(date)
    }
    // 开始时间
    const startTime = new Date(date.getFullYear(), date.getMonth(), date.getDate())
    // 结束时间
    const endTime = new Date(date.getFullYear(), date.getMonth(), date.getDate())
    endTime.setHours(23)
    endTime.setMinutes(59)
    endTime.setSeconds(59)

    return [startTime.getTime(), endTime.getTime()]
}

/**
 * 获取近七天的日期时间
 * @returns {number[]}
 */
export const getAround7DayRangeTime = () => {
    const now = new Date()
    const nowDate = now.getDate()
    const nowMonth = now.getMonth()
    const nowYear = now.getFullYear()
    const start = new Date(nowYear, nowMonth, nowDate - 6)
    const end = new Date(nowYear, nowMonth, nowDate, 23, 59, 59)
    const rangeTime = [start, end]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取近30天的日期时间
 * @returns {number[]}
 */
export const getAround30DayRangeTime = () => {
    const now = new Date()
    const nowDate = now.getDate()
    const nowMonth = now.getMonth()
    const nowYear = now.getFullYear()
    const start = new Date(nowYear, nowMonth, nowDate - 29)
    const end = new Date(nowYear, nowMonth, nowDate, 23, 59, 59)
    const rangeTime = [start, end]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取近90天的日期时间
 * @returns {number[]}
 */
export const getAround90DayRangeTime = () => {
    const now = new Date()
    const nowDate = now.getDate()
    const nowMonth = now.getMonth()
    const nowYear = now.getFullYear()
    const start = new Date(nowYear, nowMonth, nowDate - 89)
    const end = new Date(nowYear, nowMonth, nowDate, 23, 59, 59)
    const rangeTime = [start, end]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取近1个月的日期时间
 * @returns {number[]}
 */
export const getAroundOneMonthRangeTime = () => {
    const now = new Date()
    const nowMonth = now.getMonth()
    const nowYear = now.getFullYear()
    const start = new Date(nowYear, nowMonth, 1)
    const end = new Date(nowYear, nowMonth + 1, 0, 23, 59, 59)
    const rangeTime = [start, end]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取近3个月的日期时间
 * @returns {number[]}
 */
export const getAroundThreeMonthRangeTime = () => {
    const now = new Date()
    const nowMonth = now.getMonth()
    const nowYear = now.getFullYear()
    const start = new Date(nowYear, nowMonth - 3, 1)
    const end = new Date(nowYear, nowMonth + 1, 0, 23, 59, 59)
    const rangeTime = [start, end]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取半年日期时间
 * @returns {number[]}
 */
export const getAroundHalfYearRangeTime = () => {
    const end = new Date()
    end.setHours(23)
    end.setMinutes(59)
    end.setSeconds(59)
    const start = new Date()
    start.setMonth(start.getMonth() - 6)
    const rangeTime = [start, end]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取一年日期时间
 * @returns {number[]}
 */
export const getAroundOneYearRangeTime = () => {
    const end = new Date()
    end.setHours(23)
    end.setMinutes(59)
    end.setSeconds(59)
    const start = new Date()
    start.setMonth(start.getMonth() - 12)
    const rangeTime = [start, end]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取今日的日期时间
 * @returns {number[]}
 */
export const getAroundTodayRangeTime = () => {
    const now = new Date()
    const nowDate = now.getDate()
    const nowMonth = now.getMonth()
    const nowYear = now.getFullYear()
    const start = new Date(nowYear, nowMonth, nowDate)
    const end = new Date(nowYear, nowMonth, nowDate, 23, 59, 59)

    const rangeTime = `${formatDate(start)}, ${formatDate(end)}`
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取上周的日期时间
 * @returns {number[]}
 */
export const getAroundLastWeekRangeTime = () => {
    const now = new Date()
    // 上周一的日期
    now.setDate(now.getDate() - 7 - now.getDay() + 1)
    const start = new Date(now.getFullYear(), now.getMonth(), now.getDate())
    // 上周日的日期
    now.setDate(now.getDate() + 6)
    const end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59)
    const rangeTime = `${formatDate(start)}, ${formatDate(end)}`
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取本周的日期时间
 * @returns {number[]}
 */
export const getAroundCurrentWeekRangeTime = () => {
    const now = new Date()
    // 本周一的日期
    now.setDate(now.getDate() - now.getDay() + 1)
    const start = new Date(now.getFullYear(), now.getMonth(), now.getDate())
    // 本周日的日期
    now.setDate(now.getDate() + 6)
    const end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59)
    const rangeTime = `${formatDate(start)}, ${formatDate(end)}`
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取上月的日期时间
 * @returns {number[]}
 */
export const getAroundLastMonthRangeTime = () => {
    const now = new Date()
    now.setMonth(now.getMonth() - 1)

    const start = new Date(now.getFullYear(), now.getMonth(), 1)
    const end = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59)
    const rangeTime = [start, end]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取当月的日期时间
 * @returns {number[]}
 */
export const getAroundCurrentMonthRangeTime = () => {
    const now = new Date()
    const start = new Date(now.getFullYear(), now.getMonth(), 1)
    const end = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59)
    const rangeTime = `${formatDate(start)}, ${formatDate(end)}`
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取当季的日期时间
 * @returns {number[]}
 */
export const getAroundCurrentQuarterRangeTime = () => {
    const time = {}
    const date = new Date()
    const month = date.getMonth()
    const quarter = Math.floor(month / 3) + 1 // 从1开始，到4
    const startMonth = quarter * 3 - 2
    const lastMonth = quarter * 3
    // console.log(startMonth, lastMonth);
    date.setMonth(startMonth - 1)
    const startDay = date.setDate(1)
    time.dateForm = dayjs(startDay).format('YYYY-MM-DD') + ' 00:00:00'
    date.setMonth(lastMonth)
    const lastDay = date.setDate(0)
    time.dateTo = dayjs(lastDay).format('YYYY-MM-DD') + ' 23:59:59'

    const rangeTime = [time.dateForm, time.dateTo]
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取今年的日期时间
 * @returns {number[]}
 */
export const getAroundCurrentYearRangeTime = () => {
    const time = {}
    const firstDay = new Date()
    firstDay.setDate(1)
    firstDay.setMonth(0)
    const lastDay = new Date()
    lastDay.setFullYear(lastDay.getFullYear() + 1)
    lastDay.setDate(0)
    lastDay.setMonth(-1)
    time.dateForm = dayjs(firstDay).format('YYYY-MM-DD') + ' 00:00:00'
    time.dateTo = dayjs(lastDay).format('YYYY-MM-DD') + ' 23:59:59'

    const rangeTime = `${time.dateForm}, ${time.dateTo}`
    // const rangeTime = `${formatDate(time.dateForm)}, ${formatDate(time.dateTo)}`
    // console.log('rangeTime', rangeTime)
    return rangeTime
}

/**
 * 获取两个日期相差天数
 * @param date_1
 * @param date_2
 * @returns {number}
 */
export const getDiffDay = (date_1: number, date_2: number) => {
    let totalDays, diffDate
    diffDate = Math.abs(date_1 - date_2)
    totalDays = Math.floor(diffDate / (1000 * 3600 * 24))
    return totalDays
}
